﻿using Bridge.Shared.Models;
using Bridge.System.Domain;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Roles.Output;
using SysConfigs;
using SysConfigs.Input;
using SysConfigs.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Bridge.System.Application
{
    public class SysConfigService : ApplicationService, ISysConfigService
    {
        private IRepository<SysConfig, Guid> _sysConfigRepository;
        public SysConfigService(IRepository<SysConfig, Guid> sysConfigRepository) {
           _sysConfigRepository = sysConfigRepository;
        }
        public async Task<ResponseResult<SysConfigOutput>> CreateSysConfig(SysConfigInput input)
        {
            if(string.IsNullOrEmpty(input.ConfigKey))
            {
                return ResponseResult<SysConfigOutput>.Fail("配置的键名不能为空！");
            }
            if (string.IsNullOrEmpty(input.ConfigValue))
            {
                return ResponseResult<SysConfigOutput>.Fail("参数的值不能为空！");
            }

            SysConfig sysConfig = new SysConfig(GuidGenerator.Create())
            {
                ConfigKey = input.ConfigKey,
                ConfigValue = input.ConfigValue,
                Remark = input.Remark,
                ConfigName = input.ConfigName
            };
            await _sysConfigRepository.InsertAsync(sysConfig);
            return  ResponseResult<SysConfigOutput>.Success("ok",sysConfig.Adapt<SysConfigOutput>());
        }

        public async Task<ResponseResult> DeleteAsync(IdsInput<Guid> ids)
        {
            var sysConfigs = (await _sysConfigRepository.GetQueryableAsync()).Where(a => ids.Ids.Contains(a.Id)).ToList();
                if (!sysConfigs.Any())
            {
                return ResponseResult.Fail("记录不存在！");
            }
               await _sysConfigRepository.DeleteManyAsync(sysConfigs);
            return ResponseResult.Success("ok");
                }

        public async Task<PageResponseResult<SysConfigOutput>> GetPageListAsync(SysConfigPageInput input)
        {
            var query = (await _sysConfigRepository.GetQueryableAsync())
                        .WhereIf(!string.IsNullOrEmpty(input.ConfigKey), a => a.ConfigKey == input.ConfigKey)
                        .WhereIf(!string.IsNullOrEmpty(input.ConfigValue), a => a.ConfigValue == input.ConfigValue)
                        .WhereIf(!string.IsNullOrEmpty(input.ConfigName), a => a.ConfigName == input.ConfigName);

            var totalCount = query.Count(); ;
            var list = await query.Skip(input.PageSize * input.PageIndex).Take(input.PageSize).ToListAsync();
            var dto = list.Adapt<List<SysConfigOutput>>();
          
            return PageResponseResult<SysConfigOutput>.Success("ok", dto, input.PageSize, input.PageIndex, totalCount);
        }

        public async Task<ResponseResult<SysConfigOutput>> GetSysConfigById(Guid Id)
        {
            var sysConfig =await (await _sysConfigRepository.GetQueryableAsync()).FirstOrDefaultAsync(a => a.Id == Id);
            if (sysConfig == null)
            {
                return ResponseResult<SysConfigOutput>.Fail("记录不存在！");
            }
            var dto = sysConfig.Adapt<SysConfigOutput>();
            return ResponseResult<SysConfigOutput>.Success("ok",dto);
        }

        public async Task<ResponseResult> UpdateAsync(UpdateSysConfigInput input)
        {
            var sysConfig =await (await _sysConfigRepository.GetQueryableAsync()).FirstOrDefaultAsync(a => a.Id == input.Id);
            if (sysConfig == null)
            {
                return ResponseResult<SysConfigOutput>.Fail("记录不存在！");
            }
            sysConfig.ConfigKey = input.ConfigKey;
            sysConfig.ConfigValue = input.ConfigValue;
            sysConfig.ConfigName = input.ConfigName;
            sysConfig.Remark    = input.Remark;
            return ResponseResult<SysConfigOutput>.Success("ok");
        }
    }
}
